package com.qhy.service.imp;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qhy.mapper.NowProjectMapper;
import com.qhy.mapper.TbProjectMapper;
import com.qhy.mapper.UserFunProMapper;
import com.qhy.mapper.ViewProUserMapper;
import com.qhy.pojo.NowProject;
import com.qhy.pojo.NowProjectExample;
import com.qhy.pojo.TbProject;
import com.qhy.pojo.TbProjectExample;
import com.qhy.pojo.TbProjectExample.Criteria;
import com.qhy.pojo.UserFunPro;
import com.qhy.pojo.UserFunProExample;
import com.qhy.pojo.ViewProUser;
import com.qhy.pojo.ViewProUserExample;
import com.qhy.service.IProService;

@Service
public class ProService implements IProService {

	@Resource
	private TbProjectMapper mapper;

	@Resource
	private NowProjectMapper nowMapper;

	@Resource
	private ViewProUserMapper proUserMapper;

	@Resource
	private UserFunProMapper funProMapper;

	public List<TbProject> queryByType(Integer pst_id, Integer type, Integer order) {
		TbProjectExample example = new TbProjectExample();
		Criteria criteria = example.createCriteria();
		if (pst_id != 0) {
			criteria.andPsPstIdEqualTo(pst_id);
		}
		if (type != 0) {
			criteria.andPsTypeEqualTo(type);
		}else {
			criteria.andPsTypeGreaterThanOrEqualTo(1);
			criteria.andPsTypeLessThanOrEqualTo(4);
		}
		if (order == 0) {

		}
		if (order == 1) {
			example.setOrderByClause("ps_getpeople");
		}
		if (order == 2) {
			example.setOrderByClause("ps_getpeople desc");
		}
		if (order == 3) {
			example.setOrderByClause("ps_getmoney/ps_money"); 
		}
		if (order == 4) {
			example.setOrderByClause("ps_getmoney/ps_money desc");
		}

		return mapper.selectByExample(example);

	}

	public TbProject addPro(TbProject project) {
		mapper.insertSelective(project);
		TbProjectExample example = new TbProjectExample();
		Criteria criteria = example.createCriteria();
		criteria.andPsFileEqualTo(project.getPsFile());
		criteria.andPsGoalEqualTo(project.getPsGoal());
		criteria.andPsAddressEqualTo(project.getPsAddress());
		criteria.andPsMoneyEqualTo(project.getPsMoney());
		criteria.andPsDaysEqualTo(project.getPsDays());
		List<TbProject> list = mapper.selectByExample(example);
		return list.size()>0?list.get(list.size() - 1):null;
	}

	public int updatePro(TbProject project) {
		int row = mapper.updateByPrimaryKeySelective(project);
		return row;
	}

	public NowProject queryById(Integer ps_id) {
		NowProjectExample example = new NowProjectExample();
		com.qhy.pojo.NowProjectExample.Criteria criteria = example.createCriteria();
		criteria.andPsIdEqualTo(ps_id);
		return nowMapper.selectByExample(example).get(0);
	}

	public List<ViewProUser> queryByUsId(Integer psUsId) {
		ViewProUserExample example = new ViewProUserExample();
		com.qhy.pojo.ViewProUserExample.Criteria criteria = example.createCriteria();
		criteria.andPsUsIdEqualTo(psUsId);
		return proUserMapper.selectByExample(example);
	}

	public List<ViewProUser> queryByUsIdAndType(Integer psUsId) {
		ViewProUserExample example = new ViewProUserExample();
		com.qhy.pojo.ViewProUserExample.Criteria criteria = example.createCriteria();
		criteria.andPsUsIdEqualTo(psUsId);
		criteria.andPsTypeEqualTo(2);
		return proUserMapper.selectByExample(example);
	}

	public List<UserFunPro> queryFunPro(Integer usid) {
		UserFunProExample example = new UserFunProExample();
		com.qhy.pojo.UserFunProExample.Criteria criteria = example.createCriteria();
		criteria.andFdUsIdEqualTo(usid);
		return funProMapper.selectByExample(example);
	}

	public void autoUpdateType() {
		TbProjectExample example = new TbProjectExample();
		Criteria criteria = example.createCriteria();
		criteria.andPsStarttimeLessThanOrEqualTo(new Date());
		criteria.andPsTypeEqualTo(1);
		List<TbProject> list = mapper.selectByExample(example);
		for(TbProject project:list) {
			TbProject p = new TbProject();
			p.setPsType(2);
			p.setPsId(project.getPsId());
			mapper.updateByPrimaryKeySelective(p);
			
		}
	}
	
	public void autoUpdateType1() {
		TbProjectExample example = new TbProjectExample();
		Criteria criteria = example.createCriteria();
		criteria.andPsEndtimeLessThan(new Date());
		criteria.andPsTypeEqualTo(2);
		List<TbProject> list = mapper.selectByExample(example);
		for(TbProject project:list) {
			if(project.getPsMoney()>=project.getPsGetmoney()) {
				TbProject p = new TbProject();
				p.setPsType(4);
				p.setPsId(project.getPsId());
				mapper.updateByPrimaryKeySelective(p);
			}
			if(project.getPsMoney()<=project.getPsGetmoney()) {
				TbProject p = new TbProject();
				p.setPsType(3);
				p.setPsId(project.getPsId());
				mapper.updateByPrimaryKeySelective(p);
			}
		}
	}

	public TbProject queryByPsId(Integer psId) {
		return mapper.selectByPrimaryKey(psId);
	}

	public int deleteById(Integer psId) {
		return mapper.deleteByPrimaryKey(psId);
	}
	
	
	
	
	

}
